package com.dns.reset.web.controller;

import com.dns.reset.web.model.DbDnsEntity;
import com.dns.reset.web.model.DbTelnetEntity;
import com.dns.reset.web.service.DbDnsService;
import com.dns.reset.web.service.DbTelnetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@RequestMapping(value = "/")
@RestController
public class MainController {
    @Autowired
    private DbDnsService dbDnsService;
    @Autowired
    private DbTelnetService dbTelnetService;
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    public ModelAndView show(){
        return new ModelAndView("dns/show");
    }
    @RequestMapping(value = "/dnsPage",method = RequestMethod.GET)
    public ModelAndView dnsPage(){
        return new ModelAndView("dns/dnsCollect");
    }
    @RequestMapping(value = "/telnetPage",method = RequestMethod.GET)
    public ModelAndView telnetPage(){
        return new ModelAndView("dns/telnetInfo");
    }

    @RequestMapping(value = "/dnsForPage")
    public Map<String,Object> dnsForPage(@RequestParam(value = "page") Integer page,@RequestParam(value = "rows") Integer rows){
        Map<String,Object> map =new HashMap<String,Object>();
        try {
            Page<DbDnsEntity> dbDnsEntityPage = dbDnsService.queryForPage(page, rows);
            map.put("total", dbDnsEntityPage.getTotalElements());
            map.put("rows", dbDnsEntityPage.getContent());
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }
    @RequestMapping(value = "/telnetForPage")
    public Map<String,Object> telnetForPage(@RequestParam(value = "page") Integer page,@RequestParam(value = "rows") Integer rows){
        Map<String,Object> map =new HashMap<String,Object>();
       try {
           Page<DbTelnetEntity> dbTelnetEntityPage = dbTelnetService.queryForPage(page, rows);
           map.put("total", dbTelnetEntityPage.getTotalElements());
           map.put("rows", dbTelnetEntityPage.getContent());
       }catch (Exception e){
           e.printStackTrace();
       }
        return map;
    }
}
